package cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxrate;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxrate.vo.TaxRatePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxrate.vo.TaxRateRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxrate.vo.TaxRateSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxrate.vo.TaxRateSimpleListRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.tax.taxrate.TaxRateDO;
import cn.iocoder.yudao.module.erp.service.business.tax.taxrate.TaxRateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 税率")
@RestController
@RequestMapping("/erp/tax-rate")
@Validated
public class TaxRateController {

    @Resource
    private TaxRateService taxRateService;

    @PostMapping("/create")
    @Operation(summary = "创建税率")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:create')")
    public CommonResult<Long> createTaxRate(@Valid @RequestBody TaxRateSaveReqVO createReqVO) {
        return success(taxRateService.createTaxRate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新税率")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:update')")
    public CommonResult<Boolean> updateTaxRate(@Valid @RequestBody TaxRateSaveReqVO updateReqVO) {
        taxRateService.updateTaxRate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除税率")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:delete')")
    public CommonResult<Boolean> deleteTaxRate(@RequestParam("id") Long id) {
        taxRateService.deleteTaxRate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得税率")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:query')")
    public CommonResult<TaxRateRespVO> getTaxRate(@RequestParam("id") Long id) {
        TaxRateDO taxRate = taxRateService.getTaxRate(id);
        return success(BeanUtils.toBean(taxRate, TaxRateRespVO.class));
    }
    @GetMapping("/simple-list")
    @Operation(summary = "获取税率下拉框")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:query')")
    public CommonResult<List<TaxRateSimpleListRespVO>> getTaxRateSimpleList(){
         List<TaxRateDO> list =  taxRateService.getTaxRateSimpleList();
         return success(BeanUtils.toBean(list,TaxRateSimpleListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得税率分页")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:query')")
    public CommonResult<PageResult<TaxRateRespVO>> getTaxRatePage(@Valid TaxRatePageReqVO pageReqVO) {
        PageResult<TaxRateDO> pageResult = taxRateService.getTaxRatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaxRateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出税率 Excel")
    @PreAuthorize("@ss.hasPermission('erp:tax-rate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaxRateExcel(@Valid TaxRatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaxRateDO> list = taxRateService.getTaxRatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "税率.xls", "数据", TaxRateRespVO.class,
                        BeanUtils.toBean(list, TaxRateRespVO.class));
    }

}